<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-8-24
 * Time: 15:52:15
 */

namespace app\admin\model;


use think\Model;

class Active extends Model
{
    protected $table = 'active';

    protected $autoWriteTimestamp = true;

    public function getShareInfoAttr($value){
        return json_decode($value,true);
    }

    public function getNeedFieldAttr($value){
        return explode(',',$value);
    }

    public function getMustFieldAttr($value){
        return explode(',',$value);
    }

    public function getLimitAttr($value){
        return explode(',',$value);
    }

    public function setLimitAttr($value){
        return implode(',',$value);
    }

    public function getLimitParamAttr($value){
        return $value?json_decode($value,true):[];
    }

    public function setLimitParamAttr($value){
        return $value?json_encode($value,JSON_UNESCAPED_UNICODE):'';
    }

    public function getWeightAttr($value){
        return json_decode($value,true);
    }

    public function getNameAttr($value){
        return htmlspecialchars($value);
    }

    public function setImagesAttr($value){
        $new_array = [];
        if(!empty($value['image'])){
            foreach($value['image'] as $key=>$item){
                $new_array[] = [
                    'image'=>$item,
                    'url'=>$value['url'][$key]
                ];
            }
        }
        return json_encode($new_array);
    }

    public function getImagesAttr($value){
        return $value?json_decode($value,true):[];
    }

    public function setBusinessImagesAttr($value){
        $new_array = [];
        if(!empty($value['business_images'])){
            foreach($value['business_images'] as $key=>$item){
                $new_array[] = [
                    'image'=>$item,
                    'url'=>$value['url'][$key]
                ];
            }
        }
        return json_encode($new_array);
    }

    public function getBusinessImagesAttr($value){
        return $value?json_decode($value,true):[];
    }

    public function setChannelIdsAttr($value){
        return $value?implode(',',$value):'';
    }

    public function getChannelIdsAttr($value){
        return $value?explode(',',$value):[];
    }

    public function setActFieldAttr($value){
        $new_array = [];
        if(!empty($value['type'])){
            foreach($value['type'] as $key=>$item){
                $new_array[] = [
                    'type'=>$item,
                    'name'=>$value['name'][$key]
                ];
            }
        }
        return json_encode($new_array);
    }

    public function getActFieldAttr($value){
        return $value?json_decode($value,true):[];
    }

    public function setBtnInfoAttr($value){
        return $value?json_encode($value,JSON_UNESCAPED_UNICODE):'';
    }

    public function getBtnInfoAttr($value){
        return $value?json_decode($value,true):[];
    }
}